package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.domain.Groupbuyingactivity;
import com.bwie.domain.TOrder;
import com.bwie.mapper.GroupbuyingactivityMapper;
import com.bwie.service.TOrderService;
import com.bwie.mapper.TOrderMapper;
import com.bwie.utils.R;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 36167
* @description 针对表【t_order(订单表)】的数据库操作Service实现
* @createDate 2025-03-19 16:28:13
*/
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder>
    implements TOrderService{

    @Autowired
    TOrderMapper orderMapper;

    @Autowired
    GroupbuyingactivityMapper groupbuyingactivityMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public R orderadd(TOrder tOrder) {
        tOrder.setTime(new Date());
        Groupbuyingactivity groupbuyingactivity = groupbuyingactivityMapper.selectById(tOrder.getGid());
        Date endtime = groupbuyingactivity.getEndtime();
        long time = endtime.getTime();
        Date time1 = tOrder.getTime();
        long time2 = time1.getTime();

        if (time2>time){

            return R.ERROR(500,"请在有效参团时间入团");
        }
        int insert = orderMapper.insert(tOrder);

        return insert>0?R.OK("订单生成"):R.ERROR(500,"订单生成失败");
    }



    @Override
    public R orderlist() {
        List<TOrder> tOrders = orderMapper.selectList(null);
        return R.OK(tOrders);
    }

    @Override
    public R xd(Integer id) {
        rabbitTemplate.convertAndSend("day3-19","xd",id+"");
        return R.OK("已经发送邮件通知");
    }

    @Override
    public R tk(Integer id) {
        rabbitTemplate.convertAndSend("day3-19","tk",id+"");
        return R.OK("已经发送邮件通知");
    }
}




